﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Principal;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using COM.Data;

namespace COM.Service
{
    public class PermissionPrincipal : GenericPrincipal, IPermissionPrincipal, IPrincipal
    {
        public ObservableCollection<PermissionRole> RoleList { get; private set; }
        public string UserCode { get; private set; }
        public string UserName { get; private set; }
        public PermissionPrincipal(string userCode, string userName, List<PermissionRole> roleList)
            : base(new PermissionIdentity(userCode), null) //roleList.Select(i => i.EnumRoleName).ToArray()
        {
            this.UserCode = userCode;
            this.UserName = userName;
            this.RoleList = new ObservableCollection<PermissionRole>();
            roleList.ForEach(i => this.RoleList.Add(i));
            this.SetIsAuthenticated();
            this.RoleList.CollectionChanged += (sender, e) =>
            {
                this.roleNameList = null;
                this.SetIsAuthenticated();
            };
        }

        /// <summary>
        /// 任何一个角色未验证过,则此账号未验证
        /// </summary>
        /// <returns></returns>
        private bool SetIsAuthenticated()
        {
            bool isAuthenticated = true;
            if (this.RoleList.Where(i => i.EnumRoleName != AnyPermissionRole.Instance.EnumRoleName).Any(i => i.IsAuthenticated == false))
                isAuthenticated = false;
            ((PermissionIdentity)this.Identity).SetIsAuthenticated(isAuthenticated);
            return isAuthenticated;
        }

        public override bool IsInRole(string role)
        {
            return this.RoleNameList.Contains(role);
        }

        public virtual XElement UserDataXML
        {
            get
            {
                var element = new XElement("v",
                                new XAttribute("u", this.UserCode),
                                new XAttribute("n", this.UserName),
                                this.RoleList.Select(r => r.RoleDataXml)
                                );
                return element;
            }
        }

        public bool IsRole<T>() where T : PermissionRole
        {
            return this.AsRole<T>() != null;
        }

        public T AsRole<T>() where T : PermissionRole
        {
            //if (this.RoleList.OfType<T>().Count() > 1)
            //    throw new AppException("角色{0}注册了{1}次", typeof(T).FullName, this.RoleList.OfType<T>().Count());
            return this.RoleList.Where(i => i.GetType() == typeof(T)).SingleOrDefault() as T;
        }


        private List<string> roleNameList = null;
        public List<string> RoleNameList
        {
            get
            {
                if (this.roleNameList == null)
                {
                    this.roleNameList = this.RoleList.Select(i => i.EnumRoleName).ToList();
                }
                return this.roleNameList;
            }
        }

    }
}


